package com.market.service.impl;

import com.market.common.exception.ServiceException;
import com.market.common.vo.ResponseCode;
import com.market.common.vo.ResponseResult;
import com.market.entity.Classes;
import com.market.mapper.ClassesMapper;
import com.market.service.ClassesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ClassesServiceImpl implements ClassesService {
    @Resource
    ClassesMapper classesMapper;


    /**
     * 查询
     * @param namesOrId
     * @return
     * @throws ServiceException
     */
    @Override
    public List<Classes> searchClasses(String namesOrId) throws ServiceException {
        List<Classes> classes = classesMapper.selectNamdesOrId(namesOrId);
        if (classes.size() > 0) {
            return classes;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * 添加
     * @param name
     * @return
     * @throws ServiceException
     */
    @Override
    public int add(String name) throws ServiceException {
        int count = 0;
        List<Classes> list = classesMapper.selectNames();
        for (Classes classes : list) {
            if (classes.getCommodityClassesName() != name) {
                int insert = classesMapper.insert(name);
                if (insert > 0) {
                    count = insert;
                    throw new ServiceException(ResponseCode.SUCCESS);
                } else {
                    throw new ServiceException(ResponseCode.ERROR);
                }

            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        }
        return count;
    }

    /**
     * 添加
     * @param classes
     * @param name
     * @return
     * @throws ServiceException
     */
    @Override
    public int addSubName(Classes classes, String name) throws ServiceException{
        int count = 0 ;
        List<Classes> list = classesMapper.selectNames();
        for (Classes classes1:list) {
            if (classes1.getCommodityClassesName() != classes.getBaseClassesName() && classes1.getCommodityClassesName() != name){
                int inserts = classesMapper.inserts(classes, name);
                if (inserts > 0){
                    count = inserts;
                    throw new ServiceException(ResponseCode.SUCCESS);
                }else {
                    throw new ServiceException(ResponseCode.ERROR);
                }
            }else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        }
        return count;
    }

    /**
     * 修改类别
     * @param classesId
     * @param classesName
     * @return
     * @throws ServiceException
     */
    @Override
    public int chagerSub(Integer classesId, String classesName)throws ServiceException {
        int updateClasses = classesMapper.updateSub(classesId, classesName);
        if (updateClasses == 0) {
            throw new ServiceException(ResponseCode.SUCCESS);
        }else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * 删除
     * @param classesId
     * @return
     * @throws ServiceException
     */
    @Override
    public int deleteClasses(Integer classesId) throws ServiceException{
        int updateClasses = classesMapper.deleteSub(classesId);
        if (updateClasses == 0) {
            throw new ServiceException(ResponseCode.SUCCESS);
        }else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }
}
